package com.penghaisoft.microwms.common.service;


import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.page.Pagination;
import com.penghaisoft.microwms.common.page.TurnPageInfo;
import com.penghaisoft.microwms.common.dto.Resp;

/**
 * Created by zxcq0 on 2017/3/2.
 * service的基类
 */
public class BaseService<T extends Pagination> {
    /**
     * @Description: 获取翻页对象
     * @author zxcq0
     * @date 2017/3/2 16:53
     * @param
     * @return
     */
    public TurnPageInfo getTurnPageInfo(int totalCount, T obj){
        TurnPageInfo pageInfo = new TurnPageInfo();
        if (totalCount > 0){
            pageInfo.setTotalCount(totalCount);
            pageInfo.setTotalPage(totalCount%obj.getPageSize()==0?totalCount/obj.getPageSize():totalCount/obj.getPageSize()+1);
        }else {
            pageInfo.setTotalCount(0);
            pageInfo.setTotalPage(0);
        }
        return pageInfo;
    }
    /**
     * @Description: 拼装翻页信息
     * @author zxcq0
     * @date 2017/3/13 16:46
     * @param
     * @return
     */
    public void assemCondition(T obj){
       obj.setStartIndex((obj.getCurrentPage()-1)*obj.getPageSize());
    }

    protected Resp success() {
        Resp resp = new Resp();
        resp.setCode(Constant.SUCCESS);
        return resp;
    }

    protected Resp fail(String msg) {
        Resp resp = new Resp();
        resp.setCode(Constant.FAIL);
        resp.setMsg(msg);
        return resp;
    }
}
